
package com.esdk.sql;

import com.esdk.esdk;
import com.esdk.utils.EasyProp;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionBuilder{
	public final static String DriverClass="driverClass",Url="url",User="user", UserName="username",Password="password";

	public static Connection createConnection(String driverclass,String url,String user,String password)
			throws SQLException,ClassNotFoundException{
		return createConnection(driverclass,url,user,password,null);
	}

	public static Connection createConnection(String driverclass,String url,String user,String password,String dbname) throws SQLException,ClassNotFoundException{
		Class.forName(driverclass);
		/*
		 * DriverManager.registerDriver(new
		 * com.microsoft.jdbc.sqlserver.SQLServerDriver());//对于mysql不是必要步骤,
		 * 但对于oracle是必要的,而mssql默认已经注册
		 */
		java.util.Properties prop=new java.util.Properties();
		prop.setProperty("user",user);
		prop.setProperty("password",password);
		if(dbname!=null)
			prop.setProperty("DatabaseName",dbname);
		prop.setProperty("SelectMethod","direct");
		Connection connection=DriverManager.getConnection(url,prop);
		esdk.sout(connection.getMetaData().getURL());
		return connection;
	}

	public static Connection createConnection(File file){
		try(FileInputStream is=new FileInputStream(file)){
			return createConnection(is);
		}catch(Exception e){
			throw esdk.tool.wrapThrowble(e);
    }

	}

	public static Connection createConnection(String propFileName){
		return createConnection(esdk.file.getInputStream(propFileName));
	}

	public static Connection createConnection(Properties prop){
		return createConnection(new EasyProp(prop));
	}

	public static Connection createConnection(InputStream is){
		Properties prop=new Properties();
		try{
			prop.load(is);
			is.close();
			return createConnection(new EasyProp(prop));
		}catch(IOException e){
			throw esdk.tool.wrapThrowble(e);
    }
  }

	public static Connection createConnection(EasyProp prop){
		java.util.Properties prams=new java.util.Properties();
		if(prop.containsKey("spring.datasource.url")) {
			prams.put(Url,prop.get("spring.datasource.url"));
			prams.put(DriverClass,prop.get("spring.datasource.driver-class-name"));
			prams.put(User,prop.get("spring.datasource.username"));
			prams.put(Password,prop.get("spring.datasource.password"));
		}else if(prop.containsKey("jdbc.url")) {
			prams.put(Url,prop.get("jdbc.url"));
			prams.put(DriverClass,prop.get("jdbc.driverClass"));
			prams.put(User,prop.get("jdbc.username"));
			prams.put(Password,prop.get("jdbc.password"));
		}else if(prop.containsKey(Url)){
			prams.put(Url,prop.get(Url));
			prams.put(DriverClass,prop.get(DriverClass));
			prams.put(User,prop.get(UserName));
			prams.put(Password,prop.get(Password));
		}
		try{
			Class.forName(prams.getProperty(DriverClass));
			prams.setProperty("SelectMethod","direct");
			Connection connection=DriverManager.getConnection(prams.getProperty(Url),prams);
			esdk.sout("jdbc链接："+connection.getMetaData().getURL()+", 账号："+prams.get(User));
			return connection;
		}catch(Exception e){
			throw esdk.tool.wrapThrowble(e);
		}
	}

}
